package vtt.android.sportnews.activities;

import vtt.android.sportnews.fragments.DetailFragment;
import vtt.android.sportnews.fragments.TinTheThaoFullListFragment;
import vtt.android.sportnews.fragments.BaseTinTheThaoFragment.OnItemSelectedListener;
import vtt.android.sportnews.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;

import com.nostra13.universalimageloader.core.ImageLoader;

public class TinTheThaoFullListActivity extends BaseActivity implements OnItemSelectedListener{
	public static final String EXTRA_URL = "url";
	public static final String EXTRA_TITLE = "title";

	private String mTitle;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    // call super for overloading method action bar
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tinthethao_full_list);
        
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			final String url = extras.getString(EXTRA_URL);
			mTitle = extras.getString(EXTRA_TITLE);
			actionBar.setTitle(mTitle);
			
			TinTheThaoFullListFragment fragment = new TinTheThaoFullListFragment();
			fragment.mUrl = url;
	        
	        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
	        ft.add(R.id.fragment_rss_list, fragment);
	        ft.commit();
		}
	}

	@Override
	protected void initActionBar() {
		super.initActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(true);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		log.d("VTT>>> onOptionsItemSelected");
		if(item.getItemId() == android.R.id.home){
			finish();
		}

		return super.onOptionsItemSelected(item);
	}

    @Override
    public void onRssItemSelected(String link) {
        DetailFragment fragment = (DetailFragment) getSupportFragmentManager().findFragmentById(R.id.detailFragment);
        // dieu kien nay dung cho truong hop table co san fragment detail trong land layout
        if (fragment != null && fragment.isInLayout()) {
            fragment.loadUrl(link);
        } else {
            Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
            intent.putExtra(DetailActivity.EXTRA_URL, link);
            startActivity(intent);
        }
    }
    
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImageLoader.getInstance().clearMemoryCache();
    }
}